<?php
namespace app\home\controller;
use \think\Controller;

class User extends Common{
    public function register(){
        if(request()->isPost()){
                $where=[
                    'uname'=>input("post.uname")
                ];
                $num=model("TkUser")->where($where)->select();
                if($num){
                    $this->error("用户已存在");
                }else{
                      $data=[
                      'uname'=>input("post.uname"),
                      'pwd'=>input("post.pwd")
                      ];
                      db('tk_user')->insert($data);
                      return redirect("login");
                }  
        }else{
            return $this->fetch();
        }
    }



    public function login(){
        if(request()->isPost()){
            $data=[
                'uname'=>input("post.uname"),
                'pwd'=>input("post.pwd"),
                // 'id'=>input("post.id"),
            ];
            $re=model("TkUser")->where($data)->find();
            if(count($re)==1){
                   $uname=input("post.uname");
                      cookie("uname",$uname);
                     return $this->redirect("home/index/index"); 
                 }else{
                return $this->error("账号密码错误","home/user/login");
            }
                
        }
        return $this->fetch();     
    }

}